home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / htmlsample / ciconbuttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.3 KB  |  141 lines

  1. /*
  2.     file CIconButtons.c
  3.     
  4.     Description:
  5.     This file contains routines used to implement the color icon buttons
  6.     displayed in the top of HTMLSample's windows.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.     by John Montbriand, 1999.
  14.  
  15.     Copyright: © 1999 by Apple Computer, Inc.
  16.     all rights reserved.
  17.     
  18.     Disclaimer:
  19.     You may incorporate this sample code into your applications without
  20.     restriction, though the sample code has been provided "AS IS" and the
  21.     responsibility for its operation is 100% yours.  However, what you are
  22.     not permitted to do is to redistribute the source as "DSC Sample Code"
  23.     after having made changes. If you're going to re-distribute the source,
  24.     we require that you make it clear in the source that the code was
  25.     descended from Apple Sample Code, but that you've made changes.
  26.     
  27.     Change History (most recent first):
  28.     10/16/99 created by John Montbriand
  29. */
  30.  
  31. #include "CIconButtons.h"
  32. #include <QuickDraw.h>
  33. #include <Icons.h>
  34. #include <Events.h>
  35. #include <Resources.h>
  36. #include <string.h>
  37.  
  38.  
  39.  
  40. /* NewCIconButton retrieves a new color icon button
  41.     resource from the resource file.  the id number
  42.     corresponds to the resource id of the CICB resource. */
  43. CIconButtonHandle NewCIconButton(short id) {
  44.     Handle theRsrc;
  45.     theRsrc = GetResource(kIconButtonType, id);
  46.         /* here, we call detach resource because we
  47.         may have many windows open with many buttons
  48.         each having a different state. */
  49.     if (theRsrc != NULL) DetachResource(theRsrc);
  50.     return (CIconButtonHandle) theRsrc;
  51. }
  52.  
  53.  
  54. /* DisposeCIconButton disposes of any structures allocated
  55.     for the color icon button allocated by NewCIconButton. */
  56. void DisposeCIconButton(CIconButtonHandle cicb) {
  57.     DisposeHandle((Handle) cicb);
  58. }
  59.  
  60.  
  61.  
  62. /* GetCIconButtonStringData returns a new handled containing
  63.     the string data copied from the color icon resource.  The
  64.     handle will contain a C-style string terminated with a zero
  65.     byte.  It is the caller's responsibility to dispose of this
  66.     handle after it has been used. */
  67. OSErr GetCIconButtonStringData(CIconButtonHandle cicb, Handle *strdata) {
  68.     char handstate;
  69.     OSErr err;
  70.     handstate = HGetState((Handle) cicb);
  71.     HLock((Handle) cicb);
  72.     err = PtrToHand((**cicb).stringdata, strdata, strlen((**cicb).stringdata) + 1);
  73.     HSetState((Handle) cicb, handstate);
  74.     return err;
  75. }
  76.  
  77.  
  78. /* SetCIconButtonPosition sets the color icon button's 
  79.     screen postion. h and v are coordinates in the
  80.     current grafport. */
  81. void SetCIconButtonPosition(CIconButtonHandle cicb, short h, short v) {
  82.     char handstate;
  83.     handstate = HGetState((Handle) cicb);
  84.     HLock((Handle) cicb);
  85.     OffsetRect(&(**cicb).bounds, h - (**cicb).bounds.left, v - (**cicb).bounds.top);
  86.     HSetState((Handle) cicb, handstate);
  87. }
  88.  
  89.  
  90. /* DrawCIconButton draws the icon button using the
  91.     as it should appear given the state specified.  state
  92.     should be either kCBdisabled, kCBup, or kCBdown.
  93.     The last state a button is drawn in affects the
  94.     result of TrackCIconButton. */
  95. void DrawCIconButton(CIconButtonHandle cicb, short state) {
  96.     CIconHandle theIcon;
  97.     char handstate;
  98.     handstate = HGetState((Handle) cicb);
  99.     HLock((Handle) cicb);
  100.     theIcon = GetCIcon((**cicb).cicnIDs[state]);
  101.     if (theIcon != NULL) {
  102.         PlotCIcon(&(**cicb).bounds, theIcon);
  103.         DisposeCIcon(theIcon);
  104.     }
  105.     (**cicb).drawnstate = state;
  106.     HSetState((Handle) cicb, handstate);
  107. }
  108.  
  109.  
  110. /* TrackCIconButton should be called whenever a click is made
  111.     inside of a color icon button.  if the last time the button
  112.     was drawn its state was not kCBup or the click is outside
  113.     of the button, then this routine returns false. */
  114. Boolean TrackCIconButton(CIconButtonHandle cicb, Point where) {
  115.     Boolean result;
  116.     char handstate;
  117.     handstate = HGetState((Handle) cicb);
  118.     HLock((Handle) cicb);
  119.     if ((**cicb).drawnstate == kCBdisabled) {
  120.         result = false;
  121.     } else if (PtInRect(where, &(**cicb).bounds)) {
  122.         Boolean isin, ishilited;
  123.         Point mouseLoc;
  124.         isin = true;
  125.         ishilited = true;
  126.         mouseLoc = where;
  127.         DrawCIconButton(cicb, kCBdown);
  128.         while (StillDown()) {
  129.             GetMouse(&mouseLoc);
  130.             isin = PtInRect(mouseLoc, &(**cicb).bounds);
  131.             if (isin != ishilited) {
  132.                 ishilited = isin;
  133.                 DrawCIconButton(cicb, ishilited ? kCBdown : kCBup);
  134.             }
  135.         }
  136.         result = ishilited;
  137.     } else result = false;
  138.     HSetState((Handle) cicb, handstate);
  139.     return result;
  140. }
  141.